
package org.open.sharedRadar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Menu extends Activity {
		
	private static final int SIMPLE_RESULT = 0;
	private static final int MAP_RESULT = 1;
	private static final int GPS_ENABLE = 2;
		
	private LocationManager lm;
	private AlertDialog.Builder dialog;
	private TextView userTxtInfo;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.menu);	
      
		dialog = new AlertDialog.Builder(this);
		dialog.setMessage("This application requires GPS, would you like to enable it?");
		dialog.setCancelable(false);			
		dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
		    	Intent GPSIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		    	startActivityForResult(GPSIntent, GPS_ENABLE);				
			}
		});				
		dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
		    	Menu.this.finish();			
			}
		});
        		
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		
		if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
		{			
			dialog.show();
		}		
     
		userTxtInfo = (TextView) findViewById(R.id.helloTextView2);
		if (CurentUserProperties.getInstance().currentUserId != null)
			userTxtInfo.setText("Bine ai venit, "+ " " + CurentUserProperties.getInstance().currentUserId + "!");
            
        Button MapButton = (Button) findViewById(R.id.btnShowRadarFriendsMap);	
                
        MapButton.setOnClickListener(new OnClickListener() {        	        	
        	public void onClick(View v) {						        		        		
        		Intent MapIntent = new Intent(Menu.this,RadarFriendsMap.class);        		    
        		startActivityForResult(MapIntent, MAP_RESULT);	
        	}
        });
                               
      Button FindCar = (Button) findViewById(R.id.btnFindCar);	
              
      FindCar.setOnClickListener(new OnClickListener() {
        	   
        	public void onClick(View v) {						        		        		
        		Intent FindCar = new Intent(Menu.this,FindCar.class);        		
        		startActivityForResult(FindCar, MAP_RESULT);	
        	}
        });
      Button ManageFriends = (Button) findViewById(R.id.btnManageFriends);	
           
      ManageFriends.setOnClickListener(new OnClickListener() {      	      	
      	public void onClick(View v) {						      		      		
      		Intent FriendsIntent = new Intent(Menu.this,ManageFriends.class);      		      		
      		startActivityForResult(FriendsIntent, SIMPLE_RESULT);	
      	}
      });
      
      Button options = (Button) findViewById(R.id.btnOptions);	
           
      options.setOnClickListener(new OnClickListener() {
      	      	
      	public void onClick(View v) {						      		      		
      		Intent optionsIntent = new Intent(Menu.this,Options.class);      		      		
      		startActivityForResult(optionsIntent, SIMPLE_RESULT);	
      	}
      }); 
      
      Bundle bundle = new Bundle();
      // add extras here..
      WebServiceSyncronizer alarm = new WebServiceSyncronizer(this, bundle, 10);
    }      
	public void onDestroy()
	{
		DBAdapter finDBAdapter = CurentUserProperties.getInstance().myDBAdapter;
		finDBAdapter.open();
		finDBAdapter.parseUserDataToFromDB(true);
		finDBAdapter.close();
		super.onDestroy();
		System.runFinalizersOnExit(true);
	    System.exit(0);		
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {		
		switch(requestCode)
		{	
			case MAP_RESULT:				
				if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
				{
					dialog.show();
				}
				break;
		}
	}		
}